home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_05 / 1n05071a < prev    next >
Text File  |  1990-09-11  |  11KB  |  535 lines

  1. TITLE inttran.asm (C) 1989, Mark C. Peterson
  2. SUBTTL All rights reserved.
  3. ;
  4. ;  Code may be used in any program provided the
  5. ;    author is credited either during program 
  6. ;    execution or in the documentation.  Source
  7. ;    code may be distributed only in combination 
  8. ;    with public domain or shareware source code.
  9. ;    Source code may be modified provided the
  10. ;    copyright notice and this message is left 
  11. ;    unchanged and all modifications are clearly 
  12. ;    documented.
  13. ;
  14. ;    I would appreciate a copy of any work which 
  15. ;    incorporates this code.
  16. ;
  17. ;    Mark C. Peterson
  18. ;    253 West St., H
  19. ;    Plantsville, CT 06579
  20. ;    (203) 276-9474
  21. ;
  22. ;  References:
  23. ;     The VNR Concise Encyclopedia of Mathematics
  24. ;        by W. Gellert, H. Hustner, M. Hellwich, 
  25. ;        and H. Kastner; Van Nostrand Reinhold Co.,
  26. ;        1975.
  27. ;
  28. ;     80386/80286 Assembly Language Programming
  29. ;        by William H. Murray, III and Chris H. 
  30. ;        Pappas; Osborne McGraw-Hill, 1986.
  31. ;        
  32. ;
  33. ;
  34.  
  35. .model medium, c
  36.  
  37.  
  38. .data
  39.  
  40. PUBLIC TrigLimit, TrigOverflow, Ln2Fg16
  41.  
  42. PiFg13         dw       6487h
  43. InvPiFg33      dd       0a2f9836eh
  44. InvPiFg16      dw       517ch
  45. Ln2Fg16        dw       0b172h
  46. TrigOverflow   dw       0
  47. TrigLimit      dd       0
  48. one            dw       ?
  49. expSign        dw       ?
  50. a              dw       ?
  51. SinNeg         dw       ?
  52. CosNeg         dw       ?
  53.  
  54.  
  55. TaylorTerm  MACRO
  56. LOCAL Ratio
  57.    add   Factorial, one
  58.    jnc   SHORT Ratio
  59.  
  60.    rcr   Factorial, 1
  61.    shr   Num, 1
  62.    shr   one, 1
  63.  
  64. Ratio:
  65.    mul   Num
  66.    div   Factorial
  67. ENDM
  68.  
  69.  
  70.  
  71. Term        equ      <ax>
  72. Num         equ      <bx>
  73. Factorial   equ      <cx>
  74. Sin         equ      <si>
  75. Cos         equ      <di>
  76. e           equ      <si>
  77. Inve        equ      <di>
  78.  
  79.  
  80. .code
  81.          
  82. SinCos086   PROC     uses si di, LoNum:WORD, HiNum:WORD, \
  83.                                 SinAddr:WORD, CosAddr:WORD
  84.    mov   ax, LoNum 
  85.    mov   dx, HiNum
  86.    
  87.    xor   cx, cx
  88.    mov   SinNeg, cx
  89.    mov   CosNeg, cx
  90.    mov   a, cx
  91.    or    dx, dx
  92.    jns   AnglePositive
  93.    
  94.    not   ax
  95.    not   dx
  96.    add   ax, 1
  97.    adc   dx, cx
  98.    mov   SinNeg, 1
  99.       
  100. AnglePositive:
  101.    mov   si, ax
  102.    mov   di, dx
  103.    mul   WORD PTR InvPiFg33
  104.    mov   bx, dx
  105.    mov   ax, di
  106.    mul   WORD PTR InvPiFg33
  107.    add   bx, ax
  108.    adc   cx, dx
  109.    mov   ax, si
  110.    mul   WORD PTR InvPiFg33 + 2
  111.    add   bx, ax
  112.    adc   cx, dx
  113.    mov   ax, di
  114.    mul   WORD PTR InvPiFg33 + 2
  115.    add   ax, cx
  116.    adc   dx, 0
  117.  
  118.    and   dx, 3
  119.    mov   a, dx
  120.  
  121.    mov   Num, ax
  122.    mov   Factorial, WORD PTR InvPiFg33 + 2
  123.    mov   one, Factorial
  124.    mov   Cos, Factorial   ; Cos = 1
  125.    mov   Sin, Num         ; Sin = Num
  126.       
  127. LoopIntSinCos:
  128.    TaylorTerm          ; Term = Num * (x/2) * (x/3) * (x/4) * . . .
  129.    sub   Cos, Term     ; Cos = 1 - Num*(x/2) + (x**4)/4! - . . .
  130.    cmp   Term, WORD PTR TrigLimit
  131.    jbe   SHORT ExitIntSinCos
  132.  
  133.    TaylorTerm
  134.    sub   Sin, Term     ; Sin = Num - Num*(x/2)*(x/3) + (x**5)/5! - . . .
  135.    cmp   Term, WORD PTR TrigLimit
  136.    jbe   SHORT ExitIntSinCos
  137.       
  138.    TaylorTerm
  139.    add   Cos, Term
  140.    cmp   Term, WORD PTR TrigLimit
  141.    jbe   SHORT ExitIntSinCos
  142.       
  143.    TaylorTerm          ; Term = Num * (x/2) * (x/3) * . . .
  144.    add   Sin, Term
  145.    cmp   Term, WORD PTR TrigLimit
  146.    jnbe  LoopIntSinCos
  147.       
  148. ExitIntSinCos:
  149.    xor   ax, ax
  150.    mov   cx, ax
  151.    cmp   Cos, WORD PTR InvPiFg33 + 2
  152.    jb    CosDivide     ; Cos < 1.0
  153.       
  154.    inc   cx            ; Cos == 1.0
  155.    jmp   StoreCos
  156.       
  157. CosDivide:
  158.    mov   dx, Cos
  159.    div   WORD PTR InvPiFg33 + 2
  160.       
  161. StoreCos:
  162.    mov   Cos, ax       ; cx:Cos
  163.  
  164.    xor   ax, ax
  165.    mov   bx, ax
  166.    cmp   Sin, WORD PTR InvPiFg33 + 2
  167.    jb    SinDivide     ; Sin < 1.0
  168.    
  169.    inc   bx            ; Sin == 1.0
  170.    jmp   StoreSin
  171.       
  172. SinDivide:
  173.    mov   dx, Sin
  174.    div   WORD PTR InvPiFg33 + 2
  175.       
  176. StoreSin:
  177.    mov   Sin, ax       ; bx:Sin
  178.  
  179.    test  a, 1
  180.    jz    ChkNegCos
  181.  
  182.    xchg  cx, bx
  183.    xchg  Sin, Cos
  184.    mov   ax, SinNeg
  185.    xchg  ax, CosNeg
  186.    mov   CosNeg, ax
  187.  
  188. ChkNegCos:
  189.    mov   ax, a
  190.    shr   al, 1
  191.    rcl   ah, 1
  192.    xor   ah, al
  193.    jz    ChkNegSin
  194.  
  195.    xor   CosNeg, 1
  196.  
  197. ChkNegSin:
  198.    test  a, 2
  199.    jz    CorrectQuad
  200.  
  201.    xor   SinNeg, 1
  202.  
  203. CorrectQuad:
  204.  
  205.    cmp   CosNeg, 1
  206.    jne   CosPolarized
  207.  
  208.    not   Cos
  209.    not   cx 
  210.    add   Cos, 1
  211.    adc   cx, 0
  212.  
  213. CosPolarized:     
  214.    mov   dx, bx
  215.    mov   bx, CosAddr
  216.    mov   WORD PTR [bx], Cos
  217.    mov   WORD PTR [bx+2], cx
  218.  
  219.    cmp   SinNeg, 1
  220.    jne   SinPolarized
  221.  
  222.    not   Sin
  223.    not   dx
  224.    add   Sin, 1
  225.    adc   dx, 0
  226.  
  227. SinPolarized:
  228.    mov   bx, SinAddr
  229.    mov   WORD PTR [bx], Sin
  230.    mov   WORD PTR [bx+2], dx
  231.    ret
  232. SinCos086      ENDP
  233.       
  234.       
  235.       
  236. _e2x   PROC
  237.    mov   expSign, 0
  238.    or    dx, dx
  239.    jns   CalcExp
  240.       
  241.    mov   expSign, 1
  242.    not   ax
  243.    not   dx
  244.    add   ax, 1
  245.    adc   dx, 0
  246.    
  247. CalcExp:
  248.    div   Ln2Fg16
  249.    mov   a, ax
  250.    mov   Num, dx       ; 0 <= Num < Ln(2)
  251.       
  252.    xor   Factorial, Factorial
  253.    stc
  254.    rcr   Factorial, 1
  255.    mov   one, Factorial
  256.    mov   e, Num
  257.    mov   Term, Num
  258.    shr   Num, 1
  259.       
  260. Loop_e2x:
  261.    TaylorTerm
  262.    add   e, Term       ; e = x + x*x/2 + (x**3)/3! + . . .
  263.    cmp   Term, WORD PTR TrigLimit
  264.    jnbe  SHORT Loop_e2x
  265.       
  266. ExitIntSinhCosh:
  267.    stc
  268.    rcr   e, 1          ; e = e + 1, fg 15
  269.    ret                 ; return e**x * (2**15), 1 < e**x < 2
  270. _e2x   ENDP
  271.       
  272.       
  273.       
  274. Exp086    PROC     uses si di, LoNum:WORD, HiNum:WORD
  275.    mov   ax, LoNum 
  276.    mov   dx, HiNum
  277.    
  278.    call  _e2x
  279.       
  280.    cmp   a, 16
  281.    jae   Overflow
  282.       
  283.    cmp   expSign, 0
  284.    jnz   NegNumber
  285.       
  286.    mov   ax, e
  287.    mov   dx, ax
  288.    inc   a
  289.    mov   cx, 16
  290.    sub   cx, a
  291.    shr   dx, cl
  292.    mov   cx, a
  293.    shl   ax, cl
  294.    jmp   ExitExp086
  295.       
  296. Overflow:
  297.    xor   ax, ax
  298.    xor   dx, dx
  299.    mov   TrigOverflow, 1
  300.    jmp   ExitExp086
  301.       
  302. NegNumber:
  303.    cmp   e, 8000h
  304.    jne   DivideE
  305.       
  306.    mov   ax, e
  307.    dec   a
  308.    jmp   ShiftE
  309.       
  310. DivideE:
  311.    xor   ax, ax
  312.    mov   dx, ax
  313.    stc
  314.    rcr   dx, 1
  315.    div   e
  316.       
  317. ShiftE:
  318.    xor   dx, dx
  319.    mov   cx, a
  320.    shr   ax, cl
  321.       
  322. ExitExp086:
  323.    ret
  324. Exp086    ENDP
  325.  
  326.  
  327.  
  328. SinhCosh086    PROC     uses si di, LoNum:WORD, HiNum:WORD, \
  329.                                    SinhAddr:WORD, CoshAddr:WORD
  330.    mov   ax, LoNum
  331.    mov   dx, HiNum
  332.  
  333.    call  _e2x
  334.  
  335.    cmp   e, 8000h
  336.    jne   InvertE        ; e > 1
  337.  
  338.    mov   dx, 1
  339.    xor   ax, ax
  340.    cmp   a, 0
  341.    jne   Shiftone
  342.  
  343.    mov   e, ax
  344.    mov   cx, ax
  345.    jmp   ChkSinhSign
  346.  
  347. Shiftone:
  348.    mov   cx, a
  349.    shl   dx, cl
  350.    dec   cx
  351.    shr   e, cl
  352.    shr   dx, 1
  353.    shr   e, 1
  354.    mov   cx, dx
  355.    sub   ax, e
  356.    sbb   dx, 0
  357.    xchg  ax, e
  358.    xchg  dx, cx
  359.    jmp   ChkSinhSign
  360.  
  361. InvertE:
  362.    xor   ax, ax         ; calc 1/e
  363.    mov   dx, 8000h
  364.    div   e
  365.  
  366.    mov   Inve, ax
  367.  
  368. ShiftE:
  369.    mov   cx, a
  370.    shr   Inve, cl
  371.    inc   cl
  372.    mov   dx, e
  373.    shl   e, cl
  374.    neg   cl
  375.    add   cl, 16
  376.    shr   dx, cl
  377.    mov   cx, dx         ; cx:e == e**Exp
  378.  
  379.    mov   ax, e          ; dx:e == e**Exp
  380.    add   ax, Inve
  381.    adc   dx, 0
  382.    shr   dx, 1
  383.    rcr   ax, 1          ; cosh(Num) = (e**Exp + 1/e**Exp) / 2
  384.  
  385.    sub   e, Inve
  386.    sbb   cx, 0
  387.    sar   cx, 1
  388.    rcr   e, 1
  389.  
  390. ChkSinhSign:
  391.    or    HiNum, 0
  392.    jns   StoreHyperbolics
  393.  
  394.    not   e
  395.    not   cx
  396.    add   e, 1
  397.    adc   cx, 0
  398.  
  399. StoreHyperbolics:
  400.    mov   bx, CoshAddr
  401.    mov   WORD PTR [bx], ax
  402.    mov   WORD PTR [bx+2], dx
  403.  
  404.    mov   bx, SinhAddr
  405.    mov   WORD PTR [bx], e
  406.    mov   WORD PTR [bx+2], cx
  407.  
  408.    ret
  409. SinhCosh086    ENDP
  410.  
  411.  
  412.  
  413. Numerator   equ      <bx>
  414. Denominator equ      <di>
  415. Counter     equ      <si>
  416.  
  417. Log086   PROC     uses si di, LoNum:WORD, HiNum:WORD, \
  418.                                Fudge:WORD
  419. LOCAL Exp:WORD, Accum:WORD, LoAns:WORD, HiAns:WORD
  420.       xor   bx, bx
  421.       mov   Accum, bx
  422.       mov   LoAns, bx
  423.       mov   HiAns, bx
  424.       mov   cx, Fudge
  425.       mov   ax, LoNum
  426.       mov   dx, HiNum
  427.  
  428.       or    dx, dx
  429.       js    Overfl